<?php

namespace app\api\controller;

use app\common\model\ArticleCollectModel;
use app\common\model\ArticleMessageModel;
use app\common\model\CarLicenseMoreModel;
use app\common\model\MemberModel;
use app\common\model\UserRecordModel;
use app\common\model\UserRefundModel;
use think\db;
use think\Request;
use app\common\model\UserCarModel;
use app\common\model\UserCollectModel;
use app\common\model\OrderModel;
use app\common\model\CarBrandModel;
use app\common\model\CarModel;
use app\common\model\CarSeriesModel;
use app\common\model\UserCarMessageModel;

class MyController extends BaseController
{


    /**
     * @param Request $request
     * @return \think\response\Json
     */
    public function index(Request $request)
    {
        $user = $request->user;

        $count=UserCarMessageModel::where('user_id',$user->uid)
            ->where('gold',1)
            ->where('status',1)
            ->where('back_gold',0)
            ->SUM("gold_money");

        $goldCount=ArticleMessageModel::where('user_id',$user->uid)
            ->where('gold',1)
            ->where('status',1)
            ->where('back_gold',0)
            ->SUM("gold_money");

        $countCar=UserCarModel::where('user_id',$user->uid)
            ->where('gold',1)
            ->where('status',1)
            ->where('back_gold',0)
            ->SUM("gold_money");

        $addCount=bcadd(bcadd($count,$goldCount,2),$countCar,2);
        $user->addCount=$addCount;

        $user->pwd=0;
        if(!empty($user->password)) $user->pwd=1;

        unset($user->password);
        unset($user->paypwd);
        unset($user->salt);
        unset($user->uid);
        if (empty($user)) {
            return backError("用户未登陆！");
        }



        return backSuccess($user, "ok");
    }

    /**
     * 我的发布 在售已售
     * @param int type 0在售 1已售 -1下架
     * @param int page 页数
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList(Request $request)
    {
        $user = $request->user;
        $type = $request->type ?: 0;
        $page = $request->page ?: 1;
        //$user = model("member")->find(5);
        if (empty($user)) {
            return backError("用户未登陆");
        }
        $user_id = $user['uid'];
        $where = ["user_id" => $user_id, "sell_status" => $type];
        $list = model("userCar")->where($where)->order('create_time desc')->page($page, 10)->select();
        return backSuccess($list, "ok");
    }

    /**
     * 删除发布的车辆
     * @param int id 发布的车辆ID
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function del(Request $request)
    {
        $id = $request->id;
        $user = $request->user;
        //$user = model("member")->find(5);
        if (empty($user)) {
            return backError("用户未登陆");
        }
        $user_id = $user["uid"];
        $where = ["id" => $id, "user_id" => $user_id];
        $userCarModel = new UserCarModel();
        $userCar = $userCarModel->where($where)->where("status != -1")->find();
        if (empty($userCar)) {
            return backError("未找到对应车辆");
        }
        $res = $userCarModel->del($id);
        return backSuccess($res, "删除成功");
    }

    /**
     * 车辆状态设为已售
     * @param int id 车辆ID
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function toSold(Request $request)
    {
        $id = $request->id;
        $user = $request->user;
        //$user = model("member")->find(5);
        if (empty($user)) {
            return backError("用户未登陆");
        }
        $user_id = $user["uid"];
        $where = ["id" => $id, "user_id" => $user_id];
        $userCar = model("userCar")->where($where)->find();
        if (empty($userCar)) {
            return backError("未找到对应车辆");
        }
        $userCar->sell_status = 1;
        $res = $userCar->save();
        return backSuccess($res, "修改成功");
    }

    public function profit(Request $request)
    {
        //推广收益，暂无对应数据表
        return backError("用户名错误！");
    }

    /**
     * 我的收藏
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function getCollect(Request $request)
    {
        $userCollect = new UserCollectModel();
        $user = $request->user;
        $uid = $user['uid'];
        $list = $userCollect->getUserCollect($uid, 3);

        //获取文章收藏
        $article = ArticleCollectModel::getALL(['user_id' => $uid], 3);

        $data = [
            'carList' => $list,
            'article' => $article,
        ];

        return backSuccess($data);
    }

    /**
     * 收藏车辆
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function getCarList(Request $request)
    {
        $user = $request->user;
        $uid = $user['uid'];

        $userCollect = new UserCollectModel();
        $list = $userCollect->getUserCollect($uid);

        return backSuccess($list);
    }

    /**
     * 我的收藏文章
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function getArtList(Request $request)
    {
        $user = $request->user;
        $uid = $user['uid'];
        //获取文章收藏
        $article = ArticleCollectModel::getALL(['user_id' => $uid]);

        return backSuccess($article);
    }

    /**
     * 删除收藏
     * @param int id 删除的收藏ID
     * @param Request $request
     * @return \think\response\Json
     * @throws \Exception
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function delCollect(Request $request)
    {
        $id = $request->id;
        $user = $request->user;
        $uid = $user['uid'];
        $userCollect = new UserCollectModel();
        $coll = $userCollect->where(["user_id" => $uid, "id" => $id])->find();
        if (empty($coll)) {
            return backError("未找到数据");
        } else {
            $res = $coll->delete();
            return backSuccess($res, "ok");
        }


    }

    /**
     * 删除收藏文章
     * @param int id 删除的收藏ID
     * @param Request $request
     * @return \think\response\Json
     * @throws \Exception
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function delArticle(Request $request)
    {
        $id = $request->id;
        $user = $request->user;
        $uid = $user['uid'];
        $userCollect = new ArticleCollectModel();
        $coll = $userCollect->where(["user_id" => $uid, "id" => $id])->find();
        if (empty($coll)) {
            return backError("未找到数据");
        } else {
            $res = $userCollect->del($id);

            if ($res['status'])
                return backSuccess($res, "ok");
            else
                return backError("删除失败！");
        }


    }

    /**
     * 我的发布
     * @param int page 页数
     * @param Request $request
     * @return \think\response\Json
     */
    public function getRelease(Request $request)
    {
        $UserCar = new UserCarModel();
        $status = $request->status ?: 0; //0在售 1已售
        $user = $request->user;
        $uid = $user['uid'];
        $where = ["sell_status" => $status, "user_id" => $uid];
        $list = $UserCar->getMyRelease($where);
        return backSuccess($list);
    }

    /**
     * 我的发布刷新时间
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception\DbException
     */
    public function refreshUserCar(Request $request){
        $id=$request->id;

        if(empty($id)) return backError('刷新失败！');

        $info=UserCarModel::field("id,refresh_time")->get($id);

        if(empty($info)) return backError('刷新失败！');

        $checkDayStr = date('Y-m-d ',time());
        $timeBegin = strtotime($checkDayStr."00:00".":00");
        $timeEnd = strtotime($checkDayStr."23:59".":59");

        $checkTime=$info->refresh_time;

//        if($checkTime >= $timeBegin && $checkTime <= $timeEnd)
//        {
//            return backError('刷新超限！');
//        }else{
            $info->refresh_time=time();

            if($info->save()){
                return backSuccess(1,'刷新成功！');
            }
            return backError('刷新失败！');
       // }
    }

    /**
     * 删除我的发布
     * @param int page 页数
     * @param Request $request
     * @return \think\response\Json
     */
    public function delRelease(Request $request)
    {
        $id = $request->id;
        $user = $request->user;
        if (empty($user)) {
            return backError("用户未登陆");
        }
        $user_id = $user["uid"];
        $where = ["id" => $id, "user_id" => $user_id];
        $userCarModel = new UserCarModel();
        $userCar = $userCarModel->where($where)->where("status != -1")->find();
        if (empty($userCar)) {
            return backError("未找到对应车辆");
        }
        $res = $userCarModel->del($id);
        return backSuccess($res, "删除成功");
    }


    public function sold()
    {

    }

    /**
     * 用户订阅列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function book(Request $request)
    {

        $user = $request->user;
        $page = $request->page ?: 1;
        $rows = $request->rows ?: 3;
        if (empty($user)) {
            return backError("用户未登陆");
        }
        $user_id = $user["uid"];
        $data = model("userBook")
            ->where(["user_id" => $user_id])
            ->page($page, $rows)
            ->where(["user_id" => $user_id])
            ->select(); //订阅
        return backSuccess($data, "ok");
    }

    /**
     * 我的订单列表
     * type  0进行中 1已完成
     * page 读取页数
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOrder(Request $request)
    {

        $user = $request->user;
        $status = $request->status ?: 0;//0进行中 1已完成
        $page = $request->page ?: 1;
        //$user = model("member")->find(5);
        if (empty($user)) {
            return backError("用户未登陆");
        }
        $user_id = $user['uid'];
        $where[] = ["user_id", "=", $user_id];
        $where[] = ["status", "<>", -1];
        $where[] = ["order_type", "=", $status];
        $order = new OrderModel();
        $list = $order->getOrder($where, 5);
        return backSuccess($list, "ok");
    }

    /**
     * 订阅管理车辆筛选
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function subscriber(Request $request)
    {
        //订阅列表
        $where = [];
        $listRows = 10;
        $page = $request->page ?: 1;
        //车型
        $car_level = $request->car_level;
        if (!empty($car_level)) {
            $where[] = ["car_level", "like", "%" . $car_level . "%"];
        }
        //牌照所在地
        $car_address = $request->car_address;
        if (!empty($car_address)) {
            $where[] = ["car_address", "=", $car_address];
        }
        //车龄
        $min_age = $request->min_age ?: 0;
        $max_age = $request->max_age;
        $where[] = ["year_old", ">=", $min_age];
        if (!empty($max_age)) {
            $where[] = ["year_old", "<=", $max_age];
        }
        //里程区间
        $min_mileage = $request->min_mileage ?: 0;
        $max_mileage = $request->max_mileage;
        $where[] = ["go_long", ">=", $min_mileage];
        if (!empty($max_mileage)) {
            $where[] = ["go_long", "<=", $max_mileage];
        }
        //排量区间
        $min_row_amount = $request->min_row_amount ?: 0;
        $max_row_amount = $request->max_row_amount;
        $where[] = ["row_amount", ">=", $min_row_amount];
        if (!empty($max_row_amount)) {
            $where[] = ["row_amount", "<=", $max_row_amount];
        }
        //排放标准
        $car_dis = $request->car_dis;
        $where[] = ["car_dis", "=", $car_dis];
        //颜色
        $car_color = $request->car_color;
        $where[] = ["car_color", "=", $car_color];
        //变速箱
        $car_case = $request->car_case;
        $where[] = ["car_case", "=", $car_case];
        //座位数
        $seat_numb = $request->seat_numb;
        $where[] = ["seat_numb", "=", $seat_numb];
        //燃油类型
        $oil_type = $request->oil_type;
        $where[] = ["oil_type", "=", $oil_type];
        //国别
        $car_country = $request->car_country;
        $where[] = ["car_country", "=", $car_country];
        //亮点配置
        $other = $request->other;
        $where[] = ["other", "like", "%" . $other . "%"];
        //排序
        //对应的订阅数据库
        $data = db::view("userCar", "*")
            ->where($where)
            ->page($page, $listRows)
            ->select();
        if (empty($data)) {
            return backError("数据未找到", 404);
        }
        return backSuccess($data);
    }

    /**
     * 获取用户当前订阅ID的车辆列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function getBook(Request $request)
    {
        $user = $request->user;
        $types = $request->types;
        $uid = $user["uid"];
        $userBookModel = db("UserBook");
        $where = [];
        $where["user_id"] = $uid;
        $list = $userBookModel->where($where)->select();
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]["counts"] = $this->searchCar($v,true);

                if(!empty($types)){
                    $list[$k]["blist"] = $this->getBookList($v['id'],$user);
                }

            }
            return backSuccess($list, "ok");
        } else {
            return backError("数据未找到", 404);
        }


    }

    /**
     * 搜索订阅车辆
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    private function searchCar($data,$countStatus=false,$pageCount=10)
    {
        $where = $this->search($data);


        //亮点配置
        $orWhere = [];
        if (!empty($data['other'])) {
            $other = json_decode($data['other'], true);

            $arr = [];
            foreach ($other as $item) {
                $arr[] = ['like', "%{$item}%"];
            }
            if(count($arr)>0){
                if(count($arr)<2){
                    $orWhere[] = ['u.add_conf','like',$arr[0][1]];
                }else{
                    $orWhere[] = ['u.add_conf', $arr, "or"];
                }
            }
        }

        $field="u.id,u.status,u.price,u.img_list,u.card_time,u.mileage,u.series_id,,u.vin,";
        $field.="car.car_id,car.series_thumb,car.car_name,car.brand,car.car_pl_l,car.car_bsxlx,car.car_hbbz,car.car_seat,car.car_rlxs";

        if($countStatus) $field="u.id";
        $model = Db::name('user_car')->alias("u")
            ->join("erc_car car", "car.car_id=u.car_id", "LEFT")
            ->field($field)
            ->where($where)
            ->where('u.status', UserCarModel::SHOW_STATUS)
            ->where($orWhere);

        if($countStatus){
            $count = $model->count();

            return $count;
        }else{
            $list = $model->order("u.refresh_time desc,u.adopt_time desc,u.hot desc")
                ->paginate($pageCount);

            $newList = [];
            foreach ($list as $item) {
                $item["series"] = CarSeriesModel::field('series_name,thumb')->where('series_id', $item["series_id"])->find();
                $item["img_list"] = json_decode($item["img_list"], true);

                if(!empty($item['vin'])){
                    $carVin=CarLicenseMoreModel::field("sale_name")->where('vin','=',$item['vin'])->find();
                    $item['car_name']=$carVin['sale_name'];
                }

//                $item["collectCount"] = UserCollectModel::field("id")
//                    ->where('user_car_id', $item["id"])
//                    ->where("status", UserCollectModel::SHOW_STATUS)
//                    ->count();
//
//                $item["msgCount"] = UserCarMessageModel::field("id")
//                    ->where('user_car_id', $item["id"])
//                    ->where("status", UserCarMessageModel::SHOW_STATUS)
//                    ->count();

                $newList[] = $item;
            }

            return $newList;
        }

    }

    /**
     * 搜索
     * @param $request
     * @return array
     */
    private function search($request)
    {
        $where = [];

        $car_level = $request['car_level']; //车辆类型

        //拍照是否本地
        if (!empty($request['car_address'])) {
            if ($request['car_address']==1) {
                $where[] = ["u.card_come", "=", "渝"];
            } else {
                $where[] = ["u.card_come", "<>", "渝"];
            }
        }

        //车龄
        if (!empty($request['year_old'])) {
            $time = date('Y-m-d', strtotime("-{$request['year_old']} year"));
            $where[] = ["u.card_time", '>=', $time];
        }

        //车辆类型
        if (!empty($car_level)) {
            $where[] = ["car.car_jg", 'like', "%{$car_level}%"];
        }

        //里程
        if (!empty($request['mileage'])) {
            $where[] = ["u.mileage", '<=', $request['mileage']];
        }

        //排量
        if (!empty($request['row_amount'])) {
            $where[] = ['car.car_pl_l', '=', $request["row_amount"]];
        }

        //颜色
        if (!empty($request['car_color'])) {
            $where[] = ["u.car_color", '=', $request['car_color']];
        }

        //排放标准
        if (!empty($request['car_dis'])) {
            $arr = [
                2 => "国II",
                3 => "国III,国IV,国V,国VI",
                4 => "国IV,国V,国VI",
                5 => "国V,国VI",
                6 => "国VI,国VII,国VIII,国IX",
            ];

            $where[] = ['car.car_hbbz', 'in', $arr[$request["car_dis"]]];
        }

        //座位数
        if (!empty($request['seat_numb'])) {
            $where[] = ['car.car_seat', '=', $request["seat_numb"]];
        }

        //油
        if (!empty($request['oil_type'])) {
            $where[] = ['car.car_rlxs', '=', $request["oil_type"]];
        }

        //变速箱
        if (!empty($request['car_case'])) {

            if ($request['car_case'] == 1) {
                $where[] = ['car.car_case', 'like', '%手%'];
            } else {
                $where[] = ["car.car_case", 'like', ['%自%', '%AMT%', '%无%', '%双%'], 'OR'];
            }
        }

        return $where;
    }

    /**
     * 订阅列表
     * @param $id
     * @param $user
     * @return \think\Paginator|\think\response\Json
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function getBookList($id,$user)
    {
        $whereArr = ["car_level", "car_address", "go_long", "car_color", "car_case", "seat_numb", "year_old"];
        $where = [
            "id" => $id,
            "user_id" => $user["uid"],
        ];
        $userBook = model("UserBook")->where($where)->find();
        if (empty($userBook)) {
            return backError("未找到数据");
        }

        $list =  $this->searchCar($userBook,false,3);
        return $list;
    }


    /**
     * 增加订阅
     * @param Request $request
     * @return \think\response\Json
     */
    public function addBook(Request $request)
    {
        $data = $request->data;
        $user = $request->user;
        $userBookModel = db("UserBook");

        $data["user_id"] = $user["uid"];
        $data["create_time"] = time();

        if (!empty($data['labArr'])) {
            $data["other"] = json_encode($data['labArr']);
            unset($data["labArr"]);
        }
        if (!empty($data['go_long'])) {
            $data["mileage"] = $data['go_long'];
            unset($data["go_long"]);
        }

        if (!empty($data['color'])) {
            $data['car_color'] = $data["color"];
            unset($data["color"]);
        }
        if (!empty($data['typeVal'])) {
            $data['car_level'] = $data["typeVal"];
            unset($data["typeVal"]);
        }
        if (!empty($data['card_come'])) {
            $data['car_address'] = $data["card_come"];
            unset($data["card_come"]);
        }
        $res = $userBookModel->insert($data);

        return backSuccess($res, "订阅成功！");
    }

    /**
     * 删除订阅
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function delBook(Request $request)
    {
        $id = $request->id;
        $user = $request->user;
        $uid = $user['uid'];
        $userBookModel = db("UserBook");
        $bookInfo = $userBookModel->where(["id" => $id, "user_id" => $uid])->find();
        if ($bookInfo) {
            $res = $userBookModel->delete($id);
            return backSuccess($res, "删除成功");
        } else {
            return backError("数据未找到", 404);
        }

    }

    /**
     * 编辑订阅
     * @param Request $request
     * @return \think\response\Json
     */
    public function editBook(Request $request)
    {
        $data = $request->data;
        $user = $request->user;
        $userBookModel = model("UserBook");

        $data["user_id"] = $user["uid"];
        $data["create_time"] = time();

        if (!empty($data['labArr'])) {
            $data["other"] = json_encode($data['labArr']);
            unset($data["labArr"]);
        }
        if (!empty($data['go_long'])) {
            $data["mileage"] = $data['go_long'];
            unset($data["go_long"]);
        }

        if (!empty($data['color'])) {
            $data['car_color'] = $data["color"];
            unset($data["color"]);
        }
        if (!empty($data['typeVal'])) {
            $data['car_level'] = $data["typeVal"];
            unset($data["typeVal"]);
        }
        if (!empty($data['card_come'])) {
            $data['car_address'] = $data["card_come"];
            unset($data["card_come"]);
        }
        $res = $userBookModel->isUpdate(true)->save($data);
        if ($res) {
            return backSuccess($res, "修改成功");
        } else {
            return backError("错误！修改失败");
        }
    }

    /**
     * 订阅列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function getBookInfo(Request $request)
    {
        $id = $request->id;
        $user = $request->user;
        $where = [
            "id" => $id,
            "user_id" => $user["uid"],
        ];
        $userBook = model("UserBook")->where($where)->find();
        if (empty($userBook)) {
            return backError("未找到数据");
        }


        $list = $this->searchCar($userBook);
        return backSuccess($list, "ok");
    }

    /**
     * 用户佣金交易记录
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function getUserRecord(Request $request)
    {
        $user_id = $request->user["uid"];
        $type = $request->type;

        $ur = new UserRecordModel();
        $list = $ur->getALL(["user_id" => $user_id,'status'=>$type]);

        return backSuccess($list);
    }

    /**
     * 佣金提现
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function userRefund(Request $request)
    {
        $user = $request->user;
        $pay_type = $request->pay_type;
        $min_money = getSystem("refund_min"); //最小提现金额

        $payArr = ["alipay", "wechat"];

        $file = 'log.txt';
        $fp = fopen($file, 'w+');
        flock($fp, LOCK_EX);
        fwrite($fp, 'here');

        Db::startTrans();
        if (!in_array($pay_type, $payArr)) {
            Db::rollback();
            flock($fp, LOCK_UN);
            fclose($fp);
            return backError("参数错误！");
        }

        if ($pay_type == "alipay" && $user["alipay_user"] == "") {
            Db::rollback();
            flock($fp, LOCK_UN);
            fclose($fp);
            return backError("请填写支付宝收款账户！");
        } else if ($pay_type == "wechat" && $user["wechat_user"] == "") {
            Db::rollback();
            flock($fp, LOCK_UN);
            fclose($fp);
            return backError("请填写微信收款账户！");
        }

        $u = MemberModel::field("distribut_money")->lock(true)->find($user["uid"]);

        if ($u["distribut_money"] <= 0) {
            Db::rollback();
            flock($fp, LOCK_UN);
            fclose($fp);
            return backError("账户余额不足！");
        }

        if ($u["distribut_money"] < $min_money) {
            Db::rollback();
            flock($fp, LOCK_UN);
            fclose($fp);
            return backError("账户余额必须大于或等于{$min_money}元才能提现！");
        }

        $money = $u->distribut_money;

        $status = MemberModel::where('uid', $user["uid"])
            ->update(["distribut_money" => 0]);

        if ($status) {

            $record = new UserRefundModel();
            $per = getSystem("refund_rate") / 100; //提现手续费
            $take = $money * $per; //提现手续费
            $rData = [
                "user_id" => $user["uid"],
                "refund_money" => $money,
                "refund_percent" => $per, //提现手续费
                "refund_system_take" => $take,
                "refund_price" => $money - $take,
                "refund_type" => $request->from,
                "refund_to" => $pay_type
            ];
            $record->adds($rData);

            //交易记录
            UserRecordModel::adds($user["uid"], 2, $money, 0, "提现", "提现");

            Db::commit();
            flock($fp, LOCK_UN);
            fclose($fp);
            return backSuccess(1, "提现成功！");
        } else {
            Db::rollback();
            flock($fp, LOCK_UN);
            fclose($fp);
            return backError("提现失败，请稍后再试！");
        }

    }
}
